package com.ttscm.srds.robot.seer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ttscm.srds.config.RobotParam;
import com.ttscm.srds.datagram.SeerDataPacket;
import com.ttscm.srds.utils.ConnectionUtil;
import com.ttscm.srds.utils.PacketUtil2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @version:
 * @author:
 * @className:
 * @packageName:
 * @description: 机器人状态 API
 * @date: 2022/07/21
 */
@Component
public class RobotStatus {
    private final static int ROBOT_PORT = 19204;

    private final static int API_TASK_STATUS_1020 = 1020;
    private final static int API_TASK_STATUS_PACKAGE_1110 = 1110;
    private final static int API_CURRENT_LOCK_1060 = 1060;
    private final static int API_STATION_1301 = 1301;
    private final static int API_LOCATION_1004 = 1004;

    @Autowired
    private RobotParam config;

    /**
     * 查询机器人导航状态
     *
     * @return
     * @throws IOException
     */
    public SeerDataPacket taskStatus1020() throws IOException {
        byte[] reqPackets = PacketUtil2.marshalSeerPacket(1, API_TASK_STATUS_1020, null);
        SeerDataPacket resPacket = ConnectionUtil.connectSeerRobot(config.getHost(), ROBOT_PORT, reqPackets);

        return resPacket;
    }

    public SeerDataPacket taskStatusPackage1110(String taskId) throws IOException {
        Map<String, Object> bodyDataArea = new HashMap<>();
        String[] taskIds = new String[]{taskId};
        bodyDataArea.put("task_ids", taskIds);
        byte[] reqPackets = PacketUtil2.marshalSeerPacket(1, API_TASK_STATUS_PACKAGE_1110, new ObjectMapper().writeValueAsString(bodyDataArea));
        SeerDataPacket resPacket = ConnectionUtil.connectSeerRobot(config.getHost(), ROBOT_PORT, reqPackets);

        return resPacket;
    }

    /**
     * 查询当前控制权所有者
     *
     * @return
     * @throws IOException
     */
    public SeerDataPacket currentLock1060() throws IOException {
        byte[] reqPackets = PacketUtil2.marshalSeerPacket(1, API_CURRENT_LOCK_1060, null);
        SeerDataPacket resPacket = ConnectionUtil.connectSeerRobot(config.getHost(), ROBOT_PORT, reqPackets);

        return resPacket;
    }

    /**
     * 地图中的站点
     *
     * @return
     * @throws IOException
     */
    public SeerDataPacket stations1301() throws IOException {
        byte[] reqPackets = PacketUtil2.marshalSeerPacket(1, API_STATION_1301, null);
        SeerDataPacket resPacket = ConnectionUtil.connectSeerRobot(config.getHost(), ROBOT_PORT, reqPackets);

        return resPacket;
    }

    /**
     * 机器人位置
     *
     * @return
     * @throws IOException
     */
    public SeerDataPacket location1004() throws IOException {
        byte[] reqPackets = PacketUtil2.marshalSeerPacket(1, API_LOCATION_1004, null);
        SeerDataPacket resPacket = ConnectionUtil.connectSeerRobot(config.getHost(), ROBOT_PORT, reqPackets);

        return resPacket;
    }

}
